package luzhin.frames;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

import luzhin.*;



/**
 * Main frame implementation using buttons for representing cells.
 * @author lacungus
 *
 */
public class ButtonFrame extends MainFrame implements ActionListener {
		
	private static final long serialVersionUID = 32904823498L;
	Board board;
	Game game;
	
	public void actionPerformed(ActionEvent event) {
		JButton pushedButton = (JButton) event.getSource();
		LineBorder lb = (LineBorder)pushedButton.getBorder();
		if (lb.getLineColor().equals(Color.RED)) {
			pushedButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));			
		} else {
			pushedButton.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
		}
	}
	
    protected void makeButton(Icon icon, GridBagLayout gridbag, GridBagConstraints c, int i, int j) {
    	JButton button = new JButton(icon);
    	String name = Position.positionToString(i, j);
    	button.setName(name);
    	gridbag.setConstraints(button, c);		
    	button.addActionListener(this);
    	mainPanel.add(button); 
    	allButtons.add(button);
    }

    private Icon getIcon(String position) {
		int pos = Position.getPosition(position);
		int color = Position.getColor(pos); 
		String path = "./images/E";
		
		if (board != null) {
			if (board.getPiece(pos) == Piece.BB)
				path = "./images/BB";
			if (board.getPiece(pos) == Piece.BK)
				path = "./images/BK";
			if (board.getPiece(pos) == Piece.BQ)
				path = "./images/BQ";
			if (board.getPiece(pos) == Piece.BR)
				path = "./images/BR";
			if (board.getPiece(pos) == Piece.BP)
				path = "./images/BP";
			if (board.getPiece(pos) == Piece.BN)
				path = "./images/BN";
	
			if (board.getPiece(pos) == Piece.WB)
				path = "./images/WB";
			if (board.getPiece(pos) == Piece.WK)
				path = "./images/WK";
			if (board.getPiece(pos) == Piece.WQ)
				path = "./images/WQ";
			if (board.getPiece(pos) == Piece.WR)
				path = "./images/WR";
			if (board.getPiece(pos) == Piece.WP)
				path = "./images/WP";
			if (board.getPiece(pos) == Piece.WN)
				path = "./images/WN";
		}
		
		if (color == 0)
			path += "B";
		else
			path += "W";
		path += ".gif";
        ImageIcon icon = new ImageIcon(path);
        return icon;
    }
    
    /**
     * 
     */
    void redraw(Game game) {
    	this.game = game;
    	this.board = game.getBoard();
    	Component[] components = mainPanel.getComponents();    	
    	for (int i=0;i<components.length;++i) {
    		JButton curButton = (JButton) components[i];    		
    		curButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));    		
    		String sPos = curButton.getName();

            System.out.println("board: " + board);

    		Move lastMove = board.getLastMove();
            System.out.println("lastMove: " + lastMove);
    		if (lastMove != null && lastMove.getTo() == Position.getPosition(sPos)) {
        		curButton.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));    		    			
    		}
	        Icon icon = getIcon(sPos);
	        curButton.setIcon(icon);
    	}
    	if (board.isWhiteToMove())
    		statusField.setText("white");
    	else
    		statusField.setText("black");
    	if (game.isOver()) {
    		if (game.getWinner() == Game.WHITE_WON) {
    			statusField.setText("White won");    		
    		}
    		if (game.getWinner() == Game.BLACK_WON) {
    			statusField.setText("Black won");    		    		
    		}
    		if (game.getWinner() == Game.DRAW) {
    			statusField.setText("Draw");    		    		
    		}
    	}
    	this.update(this.getGraphics());
    }
    
    /**
     * 
     */    
    public void addActionListener(ActionListener listener) {
    	for (JButton button : allButtons)
    		button.addActionListener(listener);
    }
        
    private void drawMenu() {    	
    	menuBar = new JMenuBar();
    	JMenu fileMenu = new JMenu("File");
    	saveItem = new JMenuItem("Save");
    	saveItem.setName("Save");
    	fileMenu.add(saveItem);    	
    	menuBar.add(fileMenu);    	    
    	this.setJMenuBar(menuBar);
    }
    
    JPanel mainPanel = null;
    JMenuItem saveItem;
    JTextField statusField;
    LinkedList<JButton> allButtons = new LinkedList<JButton>();
    JMenuBar menuBar;
    
    public void initDraw() {
    	drawMenu();
		if (mainPanel != null) {
			this.remove(mainPanel);
		}
		mainPanel = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        mainPanel.setLayout(gridbag);

        c.fill = GridBagConstraints.NONE;       
        
		for (int i=0;i<8;++i) {			
			for (int j=0;j<8;++j) {
				int pos = i * 8 + j;
				
				c.gridx = j;
				c.gridy = 9 - i;
				
		        Icon icon = getIcon(Position.positionToString(pos));		        
		        makeButton(icon, gridbag, c, i, j);		        		        
			}
		}
		this.add(mainPanel, BorderLayout.NORTH);
		this.add(statusField, BorderLayout.SOUTH);
        setVisible(true);		    	
    }
    
    /**
     * 
     */
	void drawGame(Game game) {
		this.board = game.getBoard();
		if (mainPanel != null) {
			this.remove(mainPanel);
		}
		mainPanel = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        mainPanel.setLayout(gridbag);

        c.fill = GridBagConstraints.NONE;       
        
		for (int i=0;i<8;++i) {			
			for (int j=0;j<8;++j) {
				int pos = i * 8 + j;
				
				c.gridx = j;
				c.gridy = 9 - i;
				
		        Icon icon = getIcon(Position.positionToString(pos));		        
		        makeButton(icon, gridbag, c, i, j);		        		        
			}
		}
		this.add(mainPanel, BorderLayout.NORTH);
		this.add(statusField, BorderLayout.SOUTH);
        setVisible(true);		
	}
	
	public ButtonFrame() {
		super("Chesster");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        statusField = new JTextField("white");
        Font font = new Font("xxx", Font.BOLD, 16);
        statusField.setFont(font);
        statusField.setEditable(false);
        this.setSize(640, 480);
        
        JButton rb = new JButton("dd");
        rb.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ae) {StandaloneLuzhin.getChesster().getGame().run();}});
	}
}